$(document).ready(function(){
    var ajaxLoading=false;
    $(".viewNextDevices").live("click",function(){
      if (!ajaxLoading) {
          ajaxLoading = true;
          var networkId=$("input[name='networkId']").val();
          var page=$(this).find("input[name='page']").val();
              $.ajax({
                                                    url: "viewDevices.action",
                                                    data:{
                                                        networkId:networkId,
                                                        page:page
                                                    },
                                                    cache:false,
                                                    type:"post",
                                                    success:function(data) {

                                                        $("#deviceInfo").html(data);
                                                          return false;
                                                      }
                                              });
                   ajaxLoading=false;
      }
    });
       $("#searchDevice").click(function(){
         if (!ajaxLoading) {
            ajaxLoading = true;
            var serial=$("input[name='searchDevice']").val();
            var networkId=$("input[name='networkId']").val();
            if(serial!="Serial Number"){
                var userType=$("input[name='usersType']").val();
                if(userType==1){
                    var  id=0;
                                 if(serial==''){
                                        $.ajax({
                                                                    url: "viewDevices.action",
                                                                    data:{
                                                                        networkId:networkId,
                                                                        subscriberId:id
                                                                    },
                                                                    cache:false,
                                                                    type:"post",
                                                                    success:function(data) {
                                                                        $("#deviceInfo").html(data);
                                                                          return false;
                                                                      }
                                                              });
                                 }else{
                                    $.ajax({
                                               url: "searchDevicesBySerial.action",
                                               data:{
                                                       networkId:networkId,
                                                       serial:serial,
                                                       subscriberId:id
                                                   },
                                               type:"post",
                                               cache:false,
                                               success:function(data) {
                                                        $("#deviceInfo").html(data);
                                                        return false;
                                                     }
                                           });
                                 }

                 }else if(userType==2){
                       var  id=$("input[name='searchUser']").val();
                                 if(serial==''){
                                      if(id!="Subscriber ID"){
                                        $.ajax({
                                                                    url: "viewDevices.action",
                                                                    data:{
                                                                        networkId:networkId,
                                                                        subscriberId:id
                                                                    },
                                                                    cache:false,
                                                                    type:"post",
                                                                    success:function(data) {
                                                                        $("#deviceInfo").html(data);
                                                                          return false;
                                                                      }
                                                              });
                                      }else{
                                          var id=0;
                                             $.ajax({
                                                                    url: "viewDevices.action",
                                                                    data:{
                                                                        networkId:networkId,
                                                                        subscriberId:id
                                                                    },
                                                                    cache:false,
                                                                    type:"post",
                                                                    success:function(data) {
                                                                        $("#deviceInfo").html(data);
                                                                          return false;
                                                                      }
                                                              });
                                          }
                                      } else{
                                                if(id!="Subscriber ID"){
                                                                $.ajax({
                                                                           url: "searchDevicesBySerial.action",
                                                                           data:{
                                                                                   networkId:networkId,
                                                                                   subscriberId:id,
                                                                                   serial:serial
                                                                               },
                                                                           cache:false,
                                                                           type:"post",
                                                                           success:function(data) {
                                                                                    $("#deviceInfo").html(data);
                                                                                    return false;
                                                                                 }
                                                               });
                                                          } else{
                                                               var id=0;
                                                                  $.ajax({
                                                                           url: "searchDevicesBySerial.action",
                                                                           data:{
                                                                                   networkId:networkId,
                                                                                   subscriberId:id,
                                                                                   serial:serial
                                                                               },
                                                                           cache:false,
                                                                           type:"post",
                                                                           success:function(data) {
                                                                                    $("#deviceInfo").html(data);
                                                                                    return false;
                                                                                 }
                                                               });
                                                          }
                                                     }
                                 }
                } else {
                        alert("Please,insert the serial for search!");
            }
             ajaxLoading = false;
         }
       });
}) ;